package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.LabExamineClause;
import cn.edu.lzu.service.LabExamineClauseService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: LabExamineClauseController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/21 16:07
 * @Version: 1.0
 */
@Controller
public class LabExamineClauseController {
    @Resource
    private LabExamineClauseService labExamineClauseService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取实验室检查所有条目
     * @Date: 2020/3/21 21:02
     * @Param: [currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getLabExamineClauses")
    public DataPage getLabExamineClauses(Integer examineItemId){
        System.out.println("LabExamineItemId:"+examineItemId);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage dataPage=labExamineClauseService.getLabExamineClausesByUnitId(examiner.getUnitId(), examineItemId);
        System.out.println(dataPage);
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加实验室检查项目的条目
     * @Date: 2020/3/21 21:21
     * @Param: [labExamineClause]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/submitLabExamineClause")
    public ResponseResult submitLabExamineClause(LabExamineClause labExamineClause){
        System.out.println(labExamineClause);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        labExamineClause.setUnitId(examiner.getUnitId());
        ResponseResult responseResult=labExamineClauseService.addLabExamineClause(labExamineClause);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据Id删除一条clause记录
     * @Date: 2020/3/22 13:20
     * @Param: [id]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/deleteLabClauseById")
    public ResponseResult deleteLabClauseById(Integer id){
        ResponseResult responseResult=labExamineClauseService.delLabExamineClauseById(id);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/fuzzySearchLabClause")
    public DataPage fuzzySearchLabClause(String searchInput, Integer searchType, Integer examineItemId){
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage dataPage=labExamineClauseService.getFuzzySearchLabClauses(examiner.getUnitId(), searchInput, searchType, examineItemId);
        return dataPage;

    }

}
